package com.shanghaionstar.discover.presenter;

import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.mag.PlateEngineNumEntity;
import com.onstar.cn.mag.thirds.VehicleViolationBean;
import com.onstar.cn.weight.ToastView;
import com.shanghaionstar.R;
import com.shanghaionstar.discover.iview.IVehicleViolationView;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.user.PlateAndEngineEnum;
import com.shanghaionstar.user.service.PlateEngineService;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;

import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func1;

/**
 * Created by 7828wc on 16/7/19.
 */
public class VehicleViolationPresenter extends BasePresenter<Object> {
    private PlateEngineService service;
    private IVehicleViolationView view;

    public VehicleViolationPresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
    }

    @Override
    public void setView(IMVPView<Object> view) {
        super.setView(view);
        this.view = (IVehicleViolationView) view;
        service = new PlateEngineService(view.onActivity());
        setServiceProvider(service);
    }

    public boolean verifyPlateNumber(String plateNumber, boolean isPlageNumber) {
        if (CommonUtil.isNull(plateNumber)) {
            new ToastView(view.onActivity()).showToast(view.onActivity().getString(isPlageNumber?R.string.option_wrong_plate_number_hint:R.string.option_wrong_engine_number_hint));
            return false;
        }
        if (isPlageNumber) {
            if (!CommonUtil.isPlateNumber(plateNumber)) {
                new ToastView(view.onActivity()).showToast(view.onActivity().getString(R.string.option_wrong_plate_number_hint));
                return false;
            }
        }
        return true;
    }

    public void updateVehicleInfo(String plateEngineNumber, String plateEngineFlag, boolean isQuery) {
        PlateEngineNumEntity plateEngineNumEntity = new PlateEngineNumEntity();
        plateEngineNumEntity.setAccountID(ApplicationUtil.getInstance().getAccountId());
        plateEngineNumEntity.setVin(ApplicationUtil.getInstance().getVin());
        if (!CommonUtil.isNull(plateEngineFlag)) {
            if (!CommonUtil.isNull(plateEngineFlag) && plateEngineFlag.equalsIgnoreCase(PlateAndEngineEnum.PlateNumber.name())) {
                plateEngineNumEntity.setLicensePlate(plateEngineNumber);
            } else if (!CommonUtil.isNull(plateEngineFlag) && plateEngineFlag.equalsIgnoreCase(PlateAndEngineEnum.EngineNumber.name())) {
                plateEngineNumEntity.setEngineNumber(plateEngineNumber);
            }
        }
        service.setPlateEngineNumEntity(plateEngineNumEntity, isQuery);
    }

    public void voilationQuery(PlateEngineNumEntity plateEngineNumEntity) {
        subscription = service.updateVehicleInfo(plateEngineNumEntity).flatMap(new Func1<PlateEngineNumEntity, Observable<VehicleViolationBean>>() {
            @Override
            public Observable<VehicleViolationBean> call(PlateEngineNumEntity entity) {
                return service.violationQuery(entity);
            }
        }).subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread()).subscribe(new Action1<VehicleViolationBean>() {
                    @Override
                    public void call(VehicleViolationBean o) {
                        view.onReponseSuccess(o);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //抛出Error异常
                        if(view != null && view.onActivity() != null) {
                            BusinessUtil.sendReportByClickType(view.onActivity(), FunctionNameFindId.Violationcheckfailedreport.getStringValue());
                            view.onResponseError((OnStarException) throwable);
                        }
                    }
                });
    }

    public void popAnimationWithKeyboard(final boolean isOpen) {
        Animation animation = AnimationUtils.loadAnimation(view.onActivity(), isOpen ? R.anim.bottom_enter : R.anim.bottom_out);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                view.popAnimationWithKyeboard(animation, isOpen);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

}
